package com.hc.kotlin_coroutine

import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.GlobalScope.coroutineContext
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlin.concurrent.thread

fun main1(){
    GlobalScope.launch {//GlobalScope创建
        delay(1000)//挂起函数
        println("Hello "+coroutineContext)
    }
//    thread {
//        Thread.sleep(1000)
//        println("Hello "+Thread.currentThread().name)
//    }
    println("World "+coroutineContext)
    Thread.sleep(2000)//阻塞函数
}
fun main2() {
    println("Hello "+coroutineContext)
    runBlocking {//runBlocking创建
        delay(2000)
        println("World "+coroutineContext)
    }
}
fun main3() = runBlocking {
    val job = GlobalScope.launch {
        delay(1000)
        println("Hello " + coroutineContext)
    }
    println("World "+coroutineContext)
    job.join() //等待协程结束后继续
    println("Quit")
}
fun main4() = runBlocking {
    launch {
        delay(1000)
        println("Hello1 " + coroutineContext)
    }
    launch {
        delay(1000)
        println("Hello2 " + coroutineContext)
    }
    println("World "+coroutineContext)
}
fun main() = runBlocking {
    launch {
        delay(1000)
        println("runBlocking launch "+coroutineContext)
    }
    println("runBlocking middle "+coroutineContext)
    coroutineScope {//coroutineScope创建
        launch {
            delay(2000)
            println("coroutineScope launch "+coroutineContext)
        }
        println("coroutineScope "+coroutineContext)
    }
    println("runBlocking quit "+coroutineContext)
}

